from selenium import webdriver
import time

chrome = webdriver.Chrome()
chrome.maximize_window()    # 窗口最大化


def save_screen_shot(name):
    chrome.save_screenshot(name)


class Test:
    def __init__(self, username, password):
        self.base_url = 'http://127.0.0.1:8080/'
        self.username = username
        self.password = password

    def test(self):
        self.test_register()
        time.sleep(2)
        self.test_login()
        time.sleep(2)
        self.test_home()

    def test_register(self):
        # 打开注册页面
        chrome.get(self.base_url + 'register')
        # 获取相关输入框
        input_username = chrome.find_element_by_xpath('//div[@class="login"]/form/div[1]/div/div/input')
        input_password = chrome.find_element_by_xpath('//div[@class="login"]/form/div[2]/div/div/input')
        input_password_repeat = chrome.find_element_by_xpath('//div[@class="login"]/form/div[3]/div/div/input')
        btn_register = chrome.find_element_by_xpath('//div[@class="login"]/form/div[6]/div/button[1]')
        input_username.send_keys(self.username)
        input_password.send_keys(self.password)
        input_password_repeat.send_keys(self.password)
        btn_register.click()
        save_screen_shot('register.png')

    def test_login(self):
        chrome.get(self.base_url + 'login')
        input_username = chrome.find_element_by_xpath('//div[@class="login"]/form/div[1]/div/div/input')
        input_password = chrome.find_element_by_xpath('//div[@class="login"]/form/div[2]/div/div/input')
        btn_login = chrome.find_element_by_xpath('//div[@class="login"]/form/div[3]/div/button[1]')
        input_username.send_keys(self.username)
        input_password.send_keys(self.password)
        btn_login.click()
        save_screen_shot('login.png')

    def test_home(self):
        chrome.get(self.base_url)
        time.sleep(0.5)
        chrome.execute_script('window.scrollTo(0, document.body.scrollHeight)')
        save_screen_shot('index.png')


if __name__ == '__main__':
    test = Test('user3', '111111')
    test.test()
